﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using MarioArena.World;

namespace MarioArena.HUD
{
    public class SplashMessage
    {
        private const float transparency = 0.4f; //0 = solid, 1 = transparent
        private const int textMarginBottom = 100;
        private const float animationDuration = 0.8f;
        private const float minimumSizeFactor = 0.14f; //dont reduce size more than 0.8
        private const int stickDuration = 2500; //time in milliseconds the message will stay after which it will disappear.

        private Color textColor = Color.Orange;

        private int screenWidth;
        private int screenHeight;

        private Texture2D backgroundTexture;
        private SpriteFont font;

        private List<string> messages = new List<string>();
        private Vector2 textPosition;
        private float sizeFactor = 1.0f;
        private bool animationFinished = false;

        private long animationFinishedTime;

        public void LoadContent()
        {
            backgroundTexture = new Texture2D(GameManager.GraphicsDevice, 1, 1);
            backgroundTexture.SetData(new[] { Color.White });
            font = GameManager.Content.Load<SpriteFont>("Fonts/SplashMessage");

            screenWidth = GameManager.GraphicsDevice.Viewport.Width;
            screenHeight = GameManager.GraphicsDevice.Viewport.Height;
        }

        public void Update(GameTime gameTime)
        {
            if (messages.Count > 0)
            {
                if (!animationFinished)
                {
                    float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
                    float sizeReduction = elapsedTime / animationDuration;
                    this.sizeFactor -= sizeReduction;
                    if (this.sizeFactor <= minimumSizeFactor)
                    {
                        this.sizeFactor = minimumSizeFactor;
                        this.animationFinishedTime = (long)gameTime.TotalGameTime.TotalMilliseconds;
                        this.animationFinished = true;
                    }
                    UpdateTextPosition();
                    
                }
                else
                {

                    long currentTime = (long)gameTime.TotalGameTime.TotalMilliseconds;
                    long timePassed = currentTime - animationFinishedTime;
                    //if message has been on screen for maximum time, remove it.
                    if (timePassed >= stickDuration)
                    {
                        this.messages.RemoveAt(0);
                        Reset();
                    }
                }
            }
        }

        /// <summary>
        /// Resets the animation variables so the next message will start with a large size again.
        /// </summary>
        private void Reset()
        {
            this.animationFinished = false;
            this.sizeFactor = 1.0f;
        }

        /// <summary>
        /// Updates the position of the text in order to center it on the screen
        /// </summary>
        private void UpdateTextPosition()
        {
            if (messages.Count > 0)
            {
                Vector2 textSize = (this.font.MeasureString(this.messages[0]) * this.sizeFactor);
                int textWidth = (int)textSize.X;
                int textHeight = (int)textSize.Y;

                int textPositionX = (this.screenWidth - textWidth) / 2;
                Console.WriteLine(textPositionX);
                int textPositionY = ((this.screenHeight - textHeight) / 2) - textMarginBottom;
                this.textPosition = new Vector2(textPositionX, textPositionY);
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            //if at least one message exists in the message queue
            if (messages.Count > 0)
            {
                //draw first message in the queue
                spriteBatch.DrawString(font, this.messages[0], this.textPosition, this.textColor * (1-this.sizeFactor), 0, Vector2.Zero, this.sizeFactor, SpriteEffects.None, 0);
            }
        }

        /// <summary>
        /// Shows the splash message on the screen. Multiple messages will be displayed one at a time.
        /// </summary>
        /// <param name="splashMessage"></param>
        public void Show(string splashMessage)
        {
            this.messages.Add(splashMessage);
            UpdateTextPosition();
        }
    }
}
